/**
 * 自定义函数
 * */

var adminLogin = 'adminInfo'; //登录信息


(function () {
    getHeader();
})();




// 分离header头部
function getHeader(){
    var uinfo = getAdminInfo();
    var h = '';
    h += '<div class="middle">';
    h += '<span id="item_name">动物疫病智检系统</span>';
    h += '<span id="ul">';
    h += '<div class="li">首页</div>';
    h += '<div class="li">健康科普</div>';
    h += '<div class="li">宠物医疗</div>';
    h += '<div class="li">商务合作</div>';
    h += '</span>';
    if (uinfo) {
        h += '<a href="javascript:;" id="logout" onclick="logout();">退出</a>';
        h += '<span id="loginer">登陆人：' + uinfo.uname + '</span>';
    } else {
        h += '<span id="loginer" onclick="tologin();">去登陆</span>';
    }
    h += '</div>';
    $('#header').html(h);
}


//去登录
function tologin(){
    window.location.href = '/login.html';
}


//退出
function logout(){
    delCache({key: adminLogin});
    window.location.href = '/index.html';
}


//显示字符串前几个字符
function strPrefix(str, n){
    if (str.length <= n) {
        return str;
    } else {
        return str.substring(0, n) + '...';
    }
}


//生成随机数
function randInt(min, max) {
    // 加1是因为我们想要包含max值在内
    return Math.floor(Math.random() * (max - min + 1)) + min;
}


//登录aid
function getAid(){
    var adminInfo = getAdminInfo();
    if (!adminInfo) {
        msgPop({msg: '没有登录'});
        setTimeout(function () {
            location.href = '/login.html';
        }, 1000);
        return
    }
    return adminInfo.aid;
}


//自定义弹窗
function msgPop(e){
    var cw = document.documentElement.clientWidth;
    var ch = document.documentElement.clientHeight;
    var msg = e.msg;
    var h = '';
    h += '<div style="position: fixed; top: 0; left: 0; z-index: 10000;">';
    h += '<div style="width: ' + (cw) + 'px; height: ' + (ch) + 'px; background: #000; opacity: 0.2; position: fixed; top: 0; left: 0; z-index: 10000;"></div>';
    h += '<div style="padding: 10px 20px; width: ' + (cw * 0.3) + 'px; text-align: center; background: #000; color: #FFF; border-radius: 5px; position: fixed; bottom: 20%; left: ' + ((cw * 0.7 - 40) / 2) + 'px; z-index: 10000;">'+msg+'</div>';
    h += '</div>';
    h += '<script>setTimeout(function(){$(\'#msgPop\').html(\'\');}, 2000)</script>';
    $('#msgPop').html(h);
}


//layer弹窗
function layerMsgErr(e){
    layer.msg(e,{icon: 0,time: 1000});
    return
}


//layer弹窗
function layerMsgOk(e){
    layer.msg(e,{icon: 1,time: 1000});
    return
}


//获取登录信息
function getAdminInfo(){
    return getCache(adminLogin);
}


//设置登录信息
function setAdminInfo(e){
    return setCache({key: adminLogin, val: e.val});
}


//获取地址栏中参数
function getParamByUrl(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r==null) return '';
    return  unescape(r[2]);
}


//设置缓存
function setCache(e){
    var key = e.key, val = JSON.stringify(e.val);
    window.localStorage.setItem(key, val);
}


//获取缓存
function getCache(key){
    var cache = window.localStorage.getItem(key);
    if (!cache) { return ''; }
    return JSON.parse(cache);
}


//删除指定缓存
function delCache(e){
    var key = e.key;
    window.localStorage.removeItem(key);
}


//清理缓存
function clearCache(){
    window.localStorage.clear();
}